package cn.edu.uestc.smgt.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.uestc.smgt.common.QueryBase;
import cn.edu.uestc.smgt.dao.UserMapper;
import cn.edu.uestc.smgt.pojo.Manager;
import cn.edu.uestc.smgt.pojo.User;

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;

	public void query(QueryBase querybase) {
		// TODO Auto-generated method stub
		querybase.setTotalRow(userMapper.size(querybase.getParameters()));
		querybase.setResults(userMapper.selectBySelections(querybase));
	}

	public int add(User user) {
		// TODO Auto-generated method stub
		user.setPassword("111111");
		return userMapper.insert(user);
	}

	public int delete(String username) {
		// TODO Auto-generated method stub
		return userMapper.deleteByPrimaryKey(username);
	}

	/**
	 * 二级用户登录
	 */
	public User yhLogin(String username, String password) {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		return userMapper.selectByUsernameAndPassword(user);
	}

	/**
	 * 二级用户修密码
	 * 
	 * @param username
	 * @param password
	 * @param newPwd
	 * @return
	 */
	public boolean yhChangePwd(String username, String password, String newPwd) {
		// TODO Auto-generated method stub
		User yh = new User();
		yh.setUsername(username);
		yh.setPassword(password);
		User yh_get = userMapper.selectByUsernameAndPassword(yh);
		if (yh_get == null) {
			return false;
		}
		yh_get.setPassword(newPwd);
		userMapper.updateByPrimaryKey(yh_get);
		return true;
	}
}
